<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Redis 配置文件 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Redis 配置文件</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Redis/">Redis</a>
			</span>
		
	</div>

	

	
		<h2 id="1-redis查看配置"><a href="#1-redis查看配置" class="headerlink" title="1.redis查看配置"></a>1.redis查看配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@redis ~]<span class="comment"># redis-cli</span></div><div class="line">127.0.0.1:6379&gt; CONFIG get *         <span class="comment"># 查看所有配置项</span></div><div class="line">127.0.0.1:6379&gt; CONFIG get timeout   <span class="comment"># 查看timeout</span></div><div class="line">1) <span class="string">"timeout"</span></div><div class="line">2) <span class="string">"300"</span></div><div class="line">127.0.0.1:6379&gt; CONFIG <span class="built_in">set</span> timeout 0 <span class="comment"># 重设 timeout</span></div><div class="line">OK</div><div class="line">127.0.0.1:6379&gt; CONFIG get timeout</div><div class="line">1) <span class="string">"timeout"</span></div><div class="line">2) <span class="string">"0"</span></div></pre></td></tr></table></figure>
<h2 id="2-redis-通用配置"><a href="#2-redis-通用配置" class="headerlink" title="2.redis 通用配置"></a>2.redis 通用配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">daemonize no                  <span class="comment"># 默认情况下，redis并不是以daemon形式来运行的。通过daemonize配置项可以控制redis的运行形式</span></div><div class="line">pidfile /path/to/redis.pid    <span class="comment"># 当以daemon形式运行时，redis会生成一个pid文件，默认会生成在/var/run/redis.pid</span></div><div class="line"><span class="built_in">bind</span> 192.168.1.2 10.8.4.2     <span class="comment"># 指定绑定的ip，可以有多个</span></div><div class="line">port 6379                     <span class="comment"># 指定监听端口</span></div><div class="line">unixsocket /tmp/redis.sock    <span class="comment"># 也可以监听socket</span></div><div class="line">unixsocketperm 755            <span class="comment"># 当监听socket时可以指定权限为755</span></div><div class="line">timeout 0                     <span class="comment"># 当一个redis-client一直没有请求发向server端，那么server端有权主   动关闭这个连接，可以通过timeout来设置“空闲超时时限”，0表示永不关闭。</span></div><div class="line">tcp-keepalive 0               <span class="comment"># TCP连接保活策略，可以通过tcp-keepalive配置项来进行设置，单位为秒，假如设置为60秒，则server端会每60秒向连接空闲的客户端发起一次ACK请求，以检查客户端是否已经挂掉，对于无响应的客户端则会关闭其连接。如果设置为0，则不会进行保活检测。</span></div><div class="line">loglevel notice               <span class="comment"># 日志级别，有四种debug, verbose, notice, warning</span></div><div class="line">logfile “”                    <span class="comment"># 定义日志路径，</span></div><div class="line">syslog-ident redis            <span class="comment"># 如果希望日志打印到syslog中，通过syslog-enabled来控制。另外，syslog-ident还可以让你指定syslog里的日志标志。</span></div><div class="line">syslog-facility local0        <span class="comment"># 指定syslog的设备，可以是USER或者local0-local7</span></div><div class="line">databases 16                  <span class="comment"># 设置数据库的总数量</span></div></pre></td></tr></table></figure>
<h2 id="3-redis-快照配置（rdb持久化）"><a href="#3-redis-快照配置（rdb持久化）" class="headerlink" title="3.redis 快照配置（rdb持久化）"></a>3.redis 快照配置（rdb持久化）</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">save 900 1                      <span class="comment"># 表示每15分钟且至少有1个key改变，就触发一次持久化 </span></div><div class="line">save 300 10                     <span class="comment"># 表示每5分钟且至少有10个key改变，就触发一次持久化</span></div><div class="line">save 60 10000                   <span class="comment"># 表示每60秒至少有10000个key改变，就触发一次持久</span></div><div class="line">save “”                         <span class="comment"># 这样可以禁用rdb持久化</span></div><div class="line">stop-writes-on-bgsave-error yes <span class="comment"># rdb持久化写入磁盘避免不了会出现失败的情况，默认一旦出现失败，redis会马上停止写操作。如果你觉得无所谓，那就可以使用该选项关闭这个功能。</span></div><div class="line">rdbcompression yes              <span class="comment"># 是否要压缩</span></div><div class="line">rdbchecksum yes                 <span class="comment"># 是否进行数据校验</span></div><div class="line">dbfilename dump.rdb             <span class="comment"># 定义快照文件的名字</span></div><div class="line">dir ./                          <span class="comment"># 定义快照文件储存路劲</span></div></pre></td></tr></table></figure>
<h2 id="4-redis-安全相关配置"><a href="#4-redis-安全相关配置" class="headerlink" title="4.redis 安全相关配置"></a>4.redis 安全相关配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">requirepass yanyi                      <span class="comment"># 设置redis-server的密码</span></div><div class="line">rename-command CONFIG  yanyi.config    <span class="comment"># 将CONFIG命令更名为yanyi.config，这样可以避免误操作，但如果使用了    AOF持久化，建议不要启用该功能</span></div><div class="line">rename-command CONFIG “”               <span class="comment"># 也可以后面定义为空，这样就禁掉了该CONFIG命令</span></div></pre></td></tr></table></figure>
<h2 id="5-redis-限制相关配置"><a href="#5-redis-限制相关配置" class="headerlink" title="5.redis 限制相关配置"></a>5.redis 限制相关配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">maxclients 10000              <span class="comment"># 限制最大客户端连接数</span></div><div class="line">maxmemory &lt;bytes&gt;             <span class="comment"># 设定最大内存使用数，单位是byte</span></div><div class="line">maxmemory-policy volatile-lru <span class="comment"># 指定内存移除规则</span></div><div class="line">maxmemory-samples 3           <span class="comment"># LRU算法和最小TTL算法都并非是精确的算法，而是估算值。所以你可以设置样本的大小。假如redis默认会检查三个key并选择其中LRU的那个，那么你可以改变这个key样本的数量。</span></div></pre></td></tr></table></figure>
<h2 id="6-redis-AOF-持久化相关配置"><a href="#6-redis-AOF-持久化相关配置" class="headerlink" title="6.redis AOF 持久化相关配置"></a>6.redis AOF 持久化相关配置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">appendonly no                   <span class="comment"># 如果是no，则开启aof持久化</span></div><div class="line">appendfilename “appendonly.aof” <span class="comment"># 指定aof文件名字</span></div><div class="line">appendfsync everysec            <span class="comment"># 指定fsync()调用模式，有三种no(不调用fsync),always(每次写都会调用fsync),everysec(每秒钟调用一次fsync)。第一种最快，第二种数据最安全，但性能会差一些，第三种为这种方案，默认为第三种。</span></div><div class="line">no-appendfsync-on-rewrite no    <span class="comment"># 设置yes，可避免当写入量非常大时的磁盘io阻塞</span></div><div class="line">auto-aof-rewrite-percentage 10  <span class="comment"># 规定什么情况下会触发aof重写。该值为一个比例，10表示当aof文件增幅达到10%时则会触发重写机制。</span></div><div class="line">uto-aof-rewrite-min-size 64mb   <span class="comment"># 重写会有一个条件，就是不能低于64Mb</span></div></pre></td></tr></table></figure>
<h2 id="7-redis-慢日志相关配置"><a href="#7-redis-慢日志相关配置" class="headerlink" title="7.redis 慢日志相关配置"></a>7.redis 慢日志相关配置</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;针对慢日志，你可以设置两个参数，一个是执行时长，单位是微秒，另一个是慢日志的长度。当一个新的命令被写入日志时，最老的一条会从命令日志队列中被移除。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">slowlog-log-slower-than 10000  <span class="comment"># 慢于10000ms则记录日志</span></div><div class="line">slowlog-max-len 128            <span class="comment"># 日志长度</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/Redis/2. Redis 配置文件/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
